package com.hzncc.flowable_diboot.job;

import com.diboot.scheduler.annotation.CollectThisJob;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.util.ThreadContext;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.QuartzJobBean;

@DisallowConcurrentExecution
@CollectThisJob(
        name = "同步sap客户",
        cron = "0 0/15 * * * ? "
)
public class SapSyncJob extends QuartzJobBean implements ApplicationContextAware {
    private ApplicationContext applicationContext;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        SapDataSyncService service = applicationContext.getBean(SapDataSyncService.class);
        try {
            ThreadContext.bind(new DefaultSecurityManager());
            service.syncData();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
